package com.asl.website.item.controller;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.asl.website.item.domain.TransportServiceItemDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.asl.common.annotation.Log;
import com.asl.common.core.controller.BaseController;
import com.asl.common.core.domain.AjaxResult;
import com.asl.common.enums.BusinessType;
import com.asl.website.item.domain.TTransportServiceItem;
import com.asl.website.item.service.ITTransportServiceItemService;
import com.asl.common.utils.poi.ExcelUtil;
import com.asl.common.core.page.TableDataInfo;

/**
 * 客户服务项Controller
 * 
 * @author ruoyi
 * @date 2025-05-21
 */
@RestController
@RequestMapping("/item/item")
public class TTransportServiceItemController extends BaseController
{
    @Autowired
    private ITTransportServiceItemService tTransportServiceItemService;

    /**
     * 根据运输类型获取服务项列表
     */
    @GetMapping("/list/{typeCode}")
    public AjaxResult getServiceItemsByType(
            @PathVariable("typeCode") Long typeCode,
            @RequestHeader(value = "Accept-Language", defaultValue = "zh-CN") String acceptLanguage) {

        TTransportServiceItem query = new TTransportServiceItem();
        query.setTypeCode(typeCode);

        List<TTransportServiceItem> items = tTransportServiceItemService.selectTTransportServiceItemList(query);
        if (items.isEmpty()) {
            return AjaxResult.error("未配置该类型的服务项");
        }

        List<TransportServiceItemDTO> result = items.stream()
                .map(item -> new TransportServiceItemDTO(item, acceptLanguage))
                .sorted(Comparator.comparingInt(TransportServiceItemDTO::getSortOrder))
                .collect(Collectors.toList());

        return AjaxResult.success(result);
    }

    /**
     * 查询客户服务项列表
     */
    @PreAuthorize("@ss.hasPermi('item:item:list')")
    @GetMapping("/list")
    public TableDataInfo list(TTransportServiceItem tTransportServiceItem)
    {
        startPage();
        List<TTransportServiceItem> list = tTransportServiceItemService.selectTTransportServiceItemList(tTransportServiceItem);
        return getDataTable(list);
    }

    /**
     * 导出客户服务项列表
     */
    @PreAuthorize("@ss.hasPermi('item:item:export')")
    @Log(title = "客户服务项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TTransportServiceItem tTransportServiceItem)
    {
        List<TTransportServiceItem> list = tTransportServiceItemService.selectTTransportServiceItemList(tTransportServiceItem);
        ExcelUtil<TTransportServiceItem> util = new ExcelUtil<TTransportServiceItem>(TTransportServiceItem.class);
        util.exportExcel(response, list, "客户服务项数据");
    }

    /**
     * 获取客户服务项详细信息
     */
    @PreAuthorize("@ss.hasPermi('item:item:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tTransportServiceItemService.selectTTransportServiceItemById(id));
    }

    /**
     * 新增客户服务项
     */
    @PreAuthorize("@ss.hasPermi('item:item:add')")
    @Log(title = "客户服务项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTransportServiceItem tTransportServiceItem)
    {
        return toAjax(tTransportServiceItemService.insertTTransportServiceItem(tTransportServiceItem));
    }

    /**
     * 修改客户服务项
     */
    @PreAuthorize("@ss.hasPermi('item:item:edit')")
    @Log(title = "客户服务项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTransportServiceItem tTransportServiceItem)
    {
        return toAjax(tTransportServiceItemService.updateTTransportServiceItem(tTransportServiceItem));
    }

    /**
     * 删除客户服务项
     */
    @PreAuthorize("@ss.hasPermi('item:item:remove')")
    @Log(title = "客户服务项", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tTransportServiceItemService.deleteTTransportServiceItemByIds(ids));
    }
}
